package com.yxhl.zoume.core.coupon.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.coupon.activity.CouponSelectActivity;
import com.yxhl.zoume.core.coupon.adapter.CouponSelectAdapter;
import com.yxhl.zoume.core.coupon.model.CouponSelectEvent;
import com.yxhl.zoume.core.coupon.model.CouponSelectModel;
import com.yxhl.zoume.core.coupon.model.CouponUseStatusEnum;
import com.yxhl.zoume.core.coupon.presenter.CouponSelectPresenter;
import com.yxhl.zoume.core.coupon.view.CouponSelectView;
import com.yxhl.zoume.data.http.model.coupon.CouponInfo;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.coupon.CouponSelectComponent;
import com.yxhl.zoume.di.component.coupon.DaggerCouponSelectComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.DensityUtils;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Created by vhawk on 2016/11/4.
 */

public class CouponSelectFragment extends BaseDataBindingFragment implements CouponSelectView {

    private static final String KEY_COUPON_SELECT_PARAM = "COUPON_SELECT_PARAM";
    @BindView(R.id.coupon_select_recycle_view)
    RecyclerView couponSelectRecycleView;

    @BindView(R.id.bt_coupon_unuse)
    TextView couponUnselectBt;
    @Inject
    CouponSelectPresenter presenter;
    private CouponSelectComponent component;
    private CouponSelectModel couponSelectModel;

    public static Fragment newInstance(CouponSelectModel couponSelectModel) {
        Fragment fragment = new CouponSelectFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_COUPON_SELECT_PARAM, couponSelectModel);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        handleExtraParam();
        presenter.attachView(this);
        presenter.fetchUsefulCoupon(couponSelectModel);

        onClickEvent();
        onRxBusEvent();
    }


    @Override
    public void onResume() {
        super.onResume();
        presenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        presenter.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    private void handleExtraParam() {
        couponSelectModel = (CouponSelectModel) getArguments().getSerializable(KEY_COUPON_SELECT_PARAM);
    }


    private void onClickEvent() {
        RxView.clicks(couponUnselectBt).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                CouponSelectEvent couponSelectEvent = new CouponSelectEvent();
                couponSelectEvent.setCouponUseStatusEnum(CouponUseStatusEnum.COUPON_USE_STATUS_ENUM_UNUSED);
                RxBus.getInstance().send(couponSelectEvent);
            }
        });
    }

    private void onRxBusEvent() {
        getSubscription().add(toSubscription(CouponSelectEvent.class, new Action1<CouponSelectEvent>() {
            @Override
            public void call(CouponSelectEvent couponSelectEvent) {
                ((CouponSelectActivity)component.context()).finish();
            }
        }));
    }

    @Override
    public void handleFetchUsefulCouponSucceed(List<CouponInfo> couponInfoList) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(component.context());
        CouponSelectAdapter adapter = new CouponSelectAdapter();
        couponSelectRecycleView.setLayoutManager(layoutManager);
        couponSelectRecycleView.setAdapter(adapter);
        couponSelectRecycleView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(DensityUtils.dip2px(component.context(), 8),
                        DensityUtils.dip2px(component.context(), 6),
                        DensityUtils.dip2px(component.context(), 8),
                        DensityUtils.dip2px(component.context(), 6));

            }
        });
        adapter.setSelectedCoupons(couponInfoList);
    }

    @Override
    public void handleFetchUsefulCouponFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleFetchUsefulCouponError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }

    @Override
    protected void initializeInjector() {
        component = DaggerCouponSelectComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(getActivity()))
                .build();
        component.inject(this);
    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        ViewDataBinding viewDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_coupon_select, container, false);
        return viewDataBinding.getRoot();
    }

    @Override
    protected void setupToolBar() {

    }
}
